package actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import searcher.cnRentSearcher;

import com.opensymphony.xwork2.ActionContext;

import entity.Rent;

public class SearchTwo extends BaseAction {
	public String execute() throws Exception {
		String deco = this.getDecorations();
		String floo = this.getFloor();
		String regi = this.getRegion();
		String xml = this.getIfUseXML();

		String lbStr = this.getLowerBound();
		String ubStr = this.getUpperBound();
		if (lbStr != null && lbStr.length() == 0) {
			lbStr = "-1";
		}
		if (ubStr != null && ubStr.length() == 0) {
			ubStr = "-1";
		}

		if (deco != null && deco.length() == 0) {
			deco = null;
		}
		if (floo != null && floo.length() == 0) {
			floo = null;
		}
		if (regi != null && regi.length() == 0) {
			regi = null;
		}
		cnRentSearcher temp = new cnRentSearcher(Integer.parseInt(lbStr),
				Integer.parseInt(ubStr), Integer.parseInt(this.getRoom1()),
				Integer.parseInt(this.getRoom2()), deco, floo, null, regi);
		int pageCount = temp.precnRentSearch();

		String curPage = this.getPage();
		int pagenum = 1;
		if (null != curPage) {
			pagenum = Integer.parseInt(curPage) > 1 ? Integer.parseInt(curPage)
					: 1;
			pagenum = pagenum <= pageCount ? pagenum : pageCount;
		}
		if ((xml != null && xml.compareTo("否") == 0) || xml == null || xml.length() == 0) {
			ActionContext.getContext().getSession().put("page", pagenum);
			rlt = temp.getResultatPage(pagenum);
			ActionContext.getContext().put("rlts", rlt);
			ActionContext.getContext().put("lowerBound", this.getLowerBound());
			ActionContext.getContext().put("upperBound", this.getUpperBound());
			ActionContext.getContext().put("floor", this.getFloor());
			ActionContext.getContext().put("room1", this.getRoom1());
			ActionContext.getContext().put("room2", this.getRoom2());
			ActionContext.getContext().put("region", this.getRegion());
			ActionContext.getContext()
					.put("decorations", this.getDecorations());
			return "input";

		} else if (xml.compareTo("是") == 0) {
			rlt = temp.getResultatPage(pagenum);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/xml;charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");

			return SUCCESS;

		}
		return SUCCESS;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getLowerBound() {
		return lowerBound;
	}

	public void setLowerBound(String lowerBound) {
		this.lowerBound = lowerBound;
	}

	public String getUpperBound() {
		return upperBound;
	}

	public void setUpperBound(String upperBound) {
		this.upperBound = upperBound;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getRoom1() {
		return room1;
	}

	public void setRoom1(String room1) {
		this.room1 = room1;
	}

	public String getRoom2() {
		return room2;
	}

	public void setRoom2(String room2) {
		this.room2 = room2;
	}

	public String getDecorations() {
		return decorations;
	}

	public void setDecorations(String decorations) {
		this.decorations = decorations;
	}

	public String getFloor() {
		return floor;
	}

	public void setFloor(String floor) {
		this.floor = floor;
	}

	public String getIfUseXML() {
		return ifUseXML;
	}

	public void setIfUseXML(String ifUseXML) {
		this.ifUseXML = ifUseXML;
	}

	private String lowerBound;
	private String upperBound;
	private String region;
	private String room1;
	private String room2;
	private String decorations;
	private String floor;
	private String page;
	private String ifUseXML;
	private ArrayList<Rent> rlt;
	public ArrayList<Rent> getRlt() {
		return rlt;
	}

	public void setRlt(ArrayList<Rent> rlt) {
		this.rlt = rlt;
	}
}
